package utils;

import def.ListNode;

/**
 * 单链表工具类
 */
public class LinkedListUtils {

    /**
     * 根据数组创建一个单链表
     */
    public static ListNode create(int... data) {
        ListNode head = null;
        ListNode tail = null;
        for (int i : data) {
            ListNode node = new ListNode(i);
            if (tail == null) {
                tail = node;
                head = tail;
            } else {
                tail.next = node;
                tail = node;
            }
        }
        return head;
    }

    public static String print(ListNode head) {
        ListNode ptr = head;
        String str = "";
        while (ptr != null) {
            if (ptr != head)
                str += "->";
            str += ptr.val;
            ptr = ptr.next;
        }
        System.out.println(str);
        return str;
    }

    public static void main(String[] args) {
        ListNode head = create(1, 2, 3, 4, 5);
        System.out.println(print(head));
    }
}
